package com.view.test.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.view.test.R;
import com.view.test.impl.OnItemPositionListener;
import com.view.test.model.ChannelModel;


import java.util.List;

/**
 * @Author：wangyibo
 * @Date：2021/11/22 13:46
 */
public class ChannelManagerAdapter extends RecyclerView.Adapter<ChannelManagerAdapter.ViewHolder>
        implements OnItemPositionListener {

    private Context context;
    private List<ChannelModel> channelModels;
    private boolean disableClick;


    public ChannelManagerAdapter(Context context) {
        this.context = context;
    }

    @NonNull
    @Override
    public ChannelManagerAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.channel_manager_adapter_item, parent, false);
        ViewHolder viewHolder = new ViewHolder(view);
        return viewHolder;
    }

    boolean isAnim;

    int scalePos = -1;

    @SuppressLint("NewApi")
    @Override
    public void onBindViewHolder(@NonNull final ViewHolder holder, final int position) {

        holder.name.setText(channelModels.get(position).getChannelName());

        holder.rl_layout.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                if (!disableClick) {
                    scalePos = holder.getLayoutPosition();
                    isAnim = true;
                    if (onListener != null) {
                        onListener.onLongClick(v, holder.getLayoutPosition());
                    }
                    notifyDataSetChanged();
                }
                return false;
            }
        });

        holder.rl_layout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (isAnim || disableClick) {
                    notifyItemRemoved(holder.getLayoutPosition());
                    if (onListener != null) {
                        ChannelModel channelModel = channelModels.get(holder.getLayoutPosition());
                        onListener.onClick(v, holder.getLayoutPosition(), channelModel);
                    }
                    channelModels.remove(holder.getLayoutPosition());
                }
            }
        });

        if (isAnim) {
            holder.rl_layout.setAnimation(AnimationUtils.loadAnimation(context, R.anim.show_shake_anim));
            holder.close_image.setVisibility(View.VISIBLE);
        } else {
            holder.rl_layout.clearAnimation();
            holder.close_image.setVisibility(View.GONE);
        }

        if (scalePos == position) {
            holder.rl_layout.setScaleX((float) 1.1);
            holder.rl_layout.setScaleY((float) 1.1);
            //holder.rl_layout.setBackground(context.getDrawable(R.drawable.channel_item_round_bg));
        } else {
            holder.rl_layout.setScaleX(1);
            holder.rl_layout.setScaleY(1);
        }
    }

    @Override
    public int getItemCount() {
        return channelModels == null ? 0 : channelModels.size();
    }

    @Override
    public void onItemSwap(int from, int target) {
        ChannelModel s = channelModels.get(from);
        channelModels.remove(from);
        channelModels.add(target, s);
        notifyItemMoved(from, target);
    }

    @Override
    public void onItemMoved(int position) {

    }

    @Override
    public void clearView(RecyclerView.ViewHolder hl) {
        scalePos = -1;
        ViewHolder holder = (ViewHolder) hl;
        holder.rl_layout.setScaleY(1);
        holder.rl_layout.setScaleX(1);
    }

    public void setEditStatus(boolean editStatus) {
        isAnim = editStatus;
        notifyDataSetChanged();
    }

    public void setDatas(List<ChannelModel> channelModels) {
        this.channelModels = channelModels;
        notifyDataSetChanged();
    }


    public void disableClick(boolean disableClick) {
        this.disableClick = disableClick;
    }


    public class ViewHolder extends RecyclerView.ViewHolder {

        private TextView name;
        private ImageView close_image;
        private RelativeLayout rl_layout;

        public ViewHolder(View itemView) {
            super(itemView);
            name = itemView.findViewById(R.id.name);
            close_image = itemView.findViewById(R.id.close_image);
            rl_layout = itemView.findViewById(R.id.rl_layout);
        }
    }

    public interface OnClickListener {
        void onLongClick(View v, int position);

        void onClick(View v, int position, ChannelModel channelModel);
    }

    private OnClickListener onListener;

    public void setOnLongListener(OnClickListener longListener) {
        this.onListener = longListener;
    }
}
